/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import entities.Domain;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import util.ISDV2;

/**
 *
 * @author Dragan
 */
public class MultipleRunsV2 {

    static final int POPULATION_SIZE = 20;
    static final int EVOLUTIONS_COUNT = 100;
    static final int TEST_COUNT = 5;
    static final double MAINTENANCE_COST = 0.9E22;
    static final double DERIVATION_COST = 0.25E12;//max 3-4E12
    static final double RHO = 0.15;

    public static void evolve(Domain domain, String queriesFile) throws Exception {
        String startClause = "MultipleRuns " + new Date();
        String filename = "v2/" + startClause.replace(":", "-") + ".txt";
        String log = startClause + "\n\n";
        String summary = "SUMMARY:\n";

        List<QE_Evolve> evolveTests = new ArrayList<QE_Evolve>();
        for (int i = 1; i <= TEST_COUNT; i++) {
            //evolveTests.add(new QE_Evolve("Test " + queriesFile + "_" + MAINTENANCE_COST + "_" + RHO + "_" + i, domain, EVOLUTIONS_COUNT, POPULATION_SIZE, "v2/" + queriesFile, "v2/domain_" + queriesFile + "_Pop=" + POPULATION_SIZE + "_" + MAINTENANCE_COST + "_" + RHO + "_" + i + ".txt", MAINTENANCE_COST, RHO));
        }
        for (QE_Evolve qe : evolveTests) {
            log += qe.getLog();
            summary += qe.getSummary() + "\n";
        }
        saveText(filename, summary + "\n" + log);
    }

    public static void evolveDerived(Domain domain, String oldDomain, String queriesFile) throws Exception {
        String startClause = "MultipleRuns " + new Date();
        String log = startClause + "\n\n";
        String summary = "SUMMARY:\n";
        String filename = "v2/" + startClause.replace(":", "-") + ".txt";

        List<QE_EvolveDerived> evolveDerivedTests = new ArrayList<QE_EvolveDerived>();


        for (int i = 1; i <= TEST_COUNT; i++) {
            evolveDerivedTests.add(new QE_EvolveDerived("Test " + queriesFile + " derived_" + MAINTENANCE_COST + "_" + RHO + "_" + i, domain, EVOLUTIONS_COUNT, POPULATION_SIZE, "v2/" + queriesFile, oldDomain, "v2/domain_" + queriesFile + MAINTENANCE_COST + "_" + RHO + "_" + i + ".txt", MAINTENANCE_COST, DERIVATION_COST, RHO));
        }
        for (QE_EvolveDerived qed : evolveDerivedTests) {
            log += qed.getLog();
            summary += qed.getSummary() + "\n";
        }
        saveText(filename, summary + "\n" + log);
    }

    public static void main(String[] args) throws Exception {
        Domain domain = new ISDV2().getDomain();


        //evolve(domain, "queriesID.txt");
        //evolve(domain, "queriesID.txt");
        String oldDomain = "v2/domain_IS_M=5E12/domain_queriesIS.txt_Pop=20_5.0E11_0.15_1.txt";

        oldDomain = "v2/domain_IS_M=5E12_der75_D=2.5E11_RUN=2/domain_queriesIS_75.txt5.0E11_0.15_2.txt";
        //evolveDerived(domain, oldDomain, "queriesIS_75.txt");
        oldDomain = "v2/domain_IS_M=5E12_der50_D=2.5E11_RUN=2/domain_queriesIS_50.txt5.0E11_0.15_5.txt";
        evolveDerived(domain, oldDomain, "queriesIS_50.txt");
        oldDomain = "v2/domain_IS_M=5E12_der25_D=2.5E11_RUN=2/domain_queriesIS_25.txt5.0E11_0.15_4.txt";
        //evolveDerived(domain, oldDomain, "queriesIS_25.txt");
        //oldDomain = "v2/domain_IS_M=5E12_der0_D=2.5E11_RUN=2/domain_queriesISD.txt5.0E11_0.15_1.txt";
        //evolveDerived(domain, oldDomain, "queriesISD.txt");

    }

    public static void saveText(String filename, String text) {
        try {
            PrintStream out = new PrintStream(new FileOutputStream(filename));
            out.print(text);
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}


